#include <iostream>
/**
 *  虚析构函数
 *    Base 是基类, Dervied 是派生类
 *    基类指针指向派生类对象, Base* poly = new Dervied();
 *    在调用 delete poly 时不会调用派生类的析构函数;
 *    所以需要把基类的西沟函数标注成虚函数, 即在函数名前加上 virtual
 *
 *
 * @author: caoshi
 * @date: 2021/5/9 11:03
 */
class Base {
public:
    Base() {
        std::cout << "Base constructor \n";
    }
    virtual ~Base() {
        std::cout << "Base destructor \n";
    }
};

class Dervied : public Base {
public:
    Dervied() {
        this->m_Array = new int[5];
        std::cout << "Dervied constructor \n";
    }
    ~Dervied() {
        delete[] m_Array;       // 在析构函数中释放这段内存
        std::cout << "Dervied destructor \n";
    }

private:
    int* m_Array;
};




int main() {
//    std::cout << "Hello, World!" << std::endl;
    Base* base = new Base();
    delete base;

    std::cout << "---------------------- \n";

    Dervied* dervied = new Dervied();
    delete dervied;

    std::cout << "---------------------- \n";

    Base* poly = new Dervied();
    delete poly;

    return 0;
}
